package com.cyj.controller;

import com.cyj.entity.Anime;
import com.cyj.entity.FileEntity;
import com.cyj.pojo.AnimeVo;
import com.cyj.service.AnimeService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/anime")
public class AnimeController {
    @Autowired
    private AnimeService animeService;

    @RequestMapping(value = "/animes")
    public String animes(Locale locale){
        return "animeList";
    }

    @RequestMapping(value = "/animeUser")
    public String animeUser(Locale locale){
        return "userMain";
    }

    @ResponseBody
    @RequestMapping(value = "/animeList",produces = "application/json;charset=utf-8")
    public Map animeList(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) throws IOException {
        Map map = new HashMap<>();

        PageInfo page  = animeService.selectAnimeList(map,pn,5);

        map.put("pageInfo",page);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/allAnimeList",produces = "application/json;charset=utf-8")
    public Map allAnimeList(Model model) throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();

        List<AnimeVo> list = animeService.selectAllAnimeList(map);

        if(list.size()>0){
            map.put("code", 200);
            map.put("msg", "电影信息查找成功");
            map.put("data",list);
        }else {
            map.put("code", 100);
            map.put("msg", "电影信息查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/animeBy",produces = "application/json;charset=utf-8")
    public Map animeBy(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer animeId = Integer.parseInt(request.getParameter("animeId"));

        map.put("animeId",animeId);

        Anime anime = animeService.selectAnimeBy(map);

        if(anime!=null){
            map.put("code", 200);
            map.put("msg", "电影信息查找成功");
            map.put("data",anime);
        }else {
            map.put("code", 100);
            map.put("msg", "电影信息查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnime",produces = "application/json;charset=utf-8")
    public Map updateAnime(HttpServletRequest request, FileEntity entity){

        Map<String, String[]> map = request.getParameterMap();
        Anime anime = new Anime();

        try {
            BeanUtils.populate(anime,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(anime);

        int num = animeService.updateAnime(anime);

        Map<String,Object> map2 = new HashMap<String,Object>();

        if (num>0){
            map2.put("code", 200);
            map2.put("msg", "动漫信息修改成功");
        }else {
            map2.put("code", 100);
            map2.put("msg", "动漫信息修改失败");
        }

        return map2;
    }

    @ResponseBody
    @RequestMapping(value = "/uploadPicture",produces = "application/json;charset=utf-8")
    public Map uploadPicture(HttpServletRequest request,FileEntity entity){

        //设置要存放图片的目录 (例如：c:/tomcat/webapp/upload)
        String realpath = request.getServletContext().getRealPath("/")+"upload/";

        String fileName = entity.getUploadFile().getOriginalFilename();
        System.out.println(fileName);

        //把路径封装为file对象（因为file可以判断文件或者目录是否存在，如果不存在就可以创建）
        File targetPath = new File(realpath+fileName);

        //判断文件或者路径是否存在
        if (!targetPath.exists()){
            targetPath.mkdirs();
        }
        try {
            //使用MultipartFile的transferTo方法保存从前端传过来MultipartFile对象
            entity.getUploadFile().transferTo(targetPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取要存到数据库里面的路径
        String picture = "http://192.168.44.48:8080"+request.getContextPath()+"/upload/"+fileName;
        System.out.println(picture);

        Integer animeId = Integer.parseInt(request.getParameter("animeId"));

        Anime anime = new Anime();
        anime.setAnimeId(animeId);
        anime.setPicture(picture);

        System.out.println(anime);

        int num = animeService.updateAnime(anime);

        Map<String,Object> map = new HashMap<String,Object>();

        if (num>0){
            map.put("code", 200);
            map.put("msg", "动漫封面上传成功");
        }else {
            map.put("code", 100);
            map.put("msg", "动漫封面上传失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/addAnime",produces = "application/json;charset=utf-8")
    public Map addAnime(@RequestBody Anime Anime){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(Anime);

        int num = animeService.addAnime(Anime);


        if(num>0){
            map.put("code",200);
            map.put("msg","添加动漫信息成功！");
        }else{
            map.put("code",100);
            map.put("msg","添加动漫信息失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/delAnime",produces = "application/json;charset=utf-8")
    public Map delAnime(@RequestBody Anime anime){

        Map<String,Object> map = new HashMap<String,Object>();

        anime.setIsPutaway(0);
        System.out.println(anime);

        int num = animeService.updateAnime(anime);

        if(num>0){
            map.put("code",200);
            map.put("msg","动漫下架成功！");
        }else{
            map.put("code",100);
            map.put("msg","动漫下架失败！");
        }


        return map;
    }
}
